package com.youpingou.controller;

import com.youpingou.domain.User;
import com.youpingou.service.UserService;
import com.youpingou.vo.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class UserController  {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @PostMapping("/regist")
    public BaseResult regist(@RequestBody User user){
        String redisCode = redisTemplate.opsForValue().get(user.getMobile());
        String code = user.getCode();
        if (StringUtils.isNotEmpty(redisCode)){
            if (redisCode.equals(code)){
                //验证成功，删除key
                redisTemplate.delete(user.getMobile());
                //保存用户
                userService.save(user);
                return BaseResult.ok();
            }
        }
        return BaseResult.error("注册失败");

    }

    @GetMapping("query")
    public User findUserByMobile(@RequestParam("mobile") String mobile,@RequestParam("password") String password){
        User user = userService.findUserByMobileAndPassword(mobile, password);

        return user;
    }


}
